package fichier;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * 
 */

/**
 * @author Benjamin GODDE & Quentin HAMMON
 *
 */
@SuppressWarnings("serial")
public class Connexion extends JFrame implements ActionListener, KeyListener {

	private User user;
	private String login;
	private String password;
	private JButton butConnexion;
	private JButton butAnnuler;
	private JLabel loginLab;
	private JLabel passwordLab;
	private JTextField loginTxt;
	private JPasswordField passwordTxt;
	private JPanel pane;



	public Connexion () {
		super("Model Manager");
		this.setBounds(450,250,350,200);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.user = new User();
		this.pane = new JPanel();
		this.user.deconnexion();
		this.login = new String ("");
		this.password = new String("");
		this.butConnexion = new JButton("Connexion");
		this.butAnnuler = new JButton("Annuler");
		this.loginLab = new JLabel ("Nom d'utilisateur");
		this.passwordLab = new JLabel("Mot de passe");
		this.loginTxt = new JTextField("");
		this.passwordTxt = new JPasswordField("");

		this.getContentPane().add(this.pane);
		this.pane.add(this.butConnexion);
		this.pane.add(this.butAnnuler);
		this.pane.add(this.loginLab);
		this.pane.add(this.passwordLab);
		this.pane.add(this.loginTxt);
		this.pane.add(this.passwordTxt);

		this.butConnexion.addActionListener(this);
		this.butAnnuler.addActionListener(this);

		this.butConnexion.addKeyListener(this);
		this.loginTxt.addKeyListener(this);
		this.passwordTxt.addKeyListener(this);

		this.pane.setBorder(BorderFactory.createTitledBorder("Connexion"));

		this.getContentPane().setLayout(null);
		this.pane.setLayout(null);

		this.pane.setBounds(5,5,340,170);

		this.loginLab.setBounds(40,40,120,20);
		this.loginTxt.setBounds(170,40,120,20);
		this.passwordLab.setBounds(40,70,120,20);
		this.passwordTxt.setBounds(170,70,120,20);
		this.butConnexion.setBounds(50,115,110,20);
		this.butAnnuler.setBounds(170,115,110,20);

		//this.butConnexion.setMnemonic('E');


		this.setResizable(false);
		this.setVisible(true);
	}

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public JButton getButConnexion() {
		return butConnexion;
	}
	public void setButConnexion(JButton butConnexion) {
		this.butConnexion = butConnexion;
	}
	public JButton getButAnnuler() {
		return butAnnuler;
	}
	public void setButAnnuler(JButton butAnnuler) {
		this.butAnnuler = butAnnuler;
	}
	public JLabel getLoginLab() {
		return loginLab;
	}
	public void setLoginLab(JLabel loginLab) {
		this.loginLab = loginLab;
	}
	public JLabel getPasswordLab() {
		return passwordLab;
	}
	public void setPasswordLab(JLabel passwordLab) {
		this.passwordLab = passwordLab;
	}
	public JTextField getLoginTxt() {
		return loginTxt;
	}
	public void setLoginTxt(JTextField loginTxt) {
		this.loginTxt = loginTxt;
	}
	public JPasswordField getPasswordTxt() {
		return passwordTxt;
	}
	public void setPasswordTxt(JPasswordField passwordTxt) {
		this.passwordTxt = passwordTxt;
	}
	public JPanel getPane() {
		return pane;
	}
	public void setPane(JPanel pane) {
		this.pane = pane;
	}
	public  void annule(){
		this.dispose();
	}

	@SuppressWarnings("deprecation")
	public void connec(){
		if(loginTxt.getText().equals("") && this.passwordTxt.getText().equals(""))
			JOptionPane.showMessageDialog(this, "Tous les champs sont vides !","Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));

		else if(loginTxt.getText().equals(""))
			JOptionPane.showMessageDialog(this, "Vous n'avez rentré aucun nom d'utilisateur.","Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));

		else if(this.passwordTxt.getText().equals(""))
			JOptionPane.showMessageDialog(this, "Vous n'avez pas rentré votre mot de passe.","Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));

		else{
			this.login = this.loginTxt.getText();
			this.password = this.passwordTxt.getText();
			user.connexion(this.login,this.password);
			if(user.getEtat() == true){
				this.dispose();
				//this.setVisible(false);
				@SuppressWarnings("unused")
				RechercheModele m = new RechercheModele();
			}
			else{
				JOptionPane.showMessageDialog(this,"Le nom d'utilisateur ou le mot de passe saisi est erroné.","Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
			}
		}
	}
	public void keyPressed(KeyEvent evt) {

		if ( evt.getKeyCode() == KeyEvent.VK_ENTER)
		{
			this.connec();
		}
	}
	public void keyTyped(KeyEvent evt) { }

	public void keyReleased(KeyEvent evt) {}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == this.butConnexion){
			this.connec();
		}

		if (e.getSource() == this.butAnnuler){
			int rep = JOptionPane.showConfirmDialog(this, "Etes-vous sûr de vouloir quitter ?", "Confirmation annulation",JOptionPane.YES_NO_OPTION);
			if (rep == JOptionPane.OK_OPTION)
				this.annule();
		}

	}

}
